package com.ruoyi.app.activity

import android.Manifest
import android.app.Activity
import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.Fragment
import com.alick.learnwebrtc.manager.WebSocketManager
import com.hjq.permissions.XXPermissions.REQUEST_CODE
import com.ruoyi.app.R
import com.ruoyi.app.adapter.FragmentPagerAdapter
import com.ruoyi.app.adapter.NavigationAdapter
import com.ruoyi.app.api.ConfigApi
import com.ruoyi.app.databinding.ActivityMainBinding
import com.ruoyi.app.fragment.ContactsFragment
import com.ruoyi.app.fragment.HomeFragment
import com.ruoyi.app.fragment.MineFragment
import com.ruoyi.app.fragment.VideoReceiveFragment
import com.ruoyi.app.fragment.VideoSendFragment
import com.ruoyi.app.fragment.WorkFragment
import com.ruoyi.app.model.Constant
import com.ruoyi.app.model.entity.ButtomItemEntity
import com.ruoyi.app.utils.LocalStorageUtils
import com.ruoyi.code.Frame
import com.ruoyi.code.base.BaseBindingActivity
import com.therouter.router.Route
import com.xuexiang.xupdate.XUpdate
import pub.devrel.easypermissions.AfterPermissionGranted
import pub.devrel.easypermissions.EasyPermissions


@Route(path = Constant.mainRoute)
class MainActivity : BaseBindingActivity<ActivityMainBinding>(),
    NavigationAdapter.OnNavigationListener {

    companion object {

        private const val INTENT_KEY_IN_FRAGMENT_INDEX: String = "fragmentIndex"
        private const val INTENT_KEY_IN_FRAGMENT_CLASS: String = "fragmentClass"
        private const val REQUEST_CODE: Int = 1000

        @JvmOverloads
        fun startActivity(
            context: Context,
            fragmentClass: Class<out Fragment>? = HomeFragment::class.java
        ) {
            val intent = Intent(context, MainActivity::class.java)
            intent.putExtra(INTENT_KEY_IN_FRAGMENT_CLASS, fragmentClass)
            if (context !is Activity) {
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
            }
            context.startActivity(intent)
        }
    }

    private var navigationAdapter: NavigationAdapter? = null
    private var pagerAdapter: FragmentPagerAdapter<Fragment>? = null

    private val list = ArrayList<ButtomItemEntity>().apply {
        add(
            ButtomItemEntity(
                Constant.theme_default_color,
                Constant.theme_select_color,
                Frame.getString(R.string.index_nav_index),
                "",
                "",
                Frame.getDrawable(R.drawable.index_selector)
            )
        )
        add(
            ButtomItemEntity(
                Constant.theme_default_color,
                Constant.theme_select_color,
                Frame.getString(R.string.work_nav_index),
                "",
                "",
                Frame.getDrawable(R.drawable.work_selector)
            )
        )
        add(
            ButtomItemEntity(
                Constant.theme_default_color,
                Constant.theme_select_color,
                Frame.getString(R.string.mine_nav_index),
                "",
                "",
                Frame.getDrawable(R.drawable.mine_selector)
            )
        )
    }

    override fun initView() {
        LocalStorageUtils.setPreference(baseContext)

        navigationAdapter = NavigationAdapter(list).apply {
            setOnNavigationListener(this@MainActivity)
            binding.rvHomeNavigation.adapter = this
        }

        pagerAdapter = FragmentPagerAdapter<Fragment>(this).apply {

            addFragment(ContactsFragment.newInstance())
//            addFragment(HomeFragment.newInstance())
//            addFragment(VideoSendFragment.newInstance())
//            addFragment(VideoReceiveFragment.newInstance())
            addFragment(WorkFragment.newInstance())
            addFragment(MineFragment.newInstance())
            binding.vpHomePager.adapter = this
        }

        onNewIntent(intent)
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        val serializable = getIntent().getSerializableExtra(INTENT_KEY_IN_FRAGMENT_CLASS)
        pagerAdapter?.let {
            if (serializable != null) {
                switchFragment(
                    it.getFragmentIndex(serializable as Class<out Fragment>)
                )
            }
        }
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        binding.vpHomePager.let {
            // 保存当前 Fragment 索引位置
            outState.putInt(INTENT_KEY_IN_FRAGMENT_INDEX, it.currentItem)
        }
    }

    override fun onRestoreInstanceState(savedInstanceState: Bundle) {
        super.onRestoreInstanceState(savedInstanceState)
        // 恢复当前 Fragment 索引位置
        switchFragment(savedInstanceState.getInt(INTENT_KEY_IN_FRAGMENT_INDEX))
    }

    override fun initData() {
        XUpdate.newBuild(this)
            .updateUrl(ConfigApi.uploadApp)
            .update()
    }

    private fun switchFragment(fragmentIndex: Int) {
        if (fragmentIndex == -1) {
            return
        }
        when (fragmentIndex) {
            0, 1, 2, 3 -> {
                binding.vpHomePager.currentItem = fragmentIndex
                navigationAdapter?.setSelectedPosition(fragmentIndex)
            }
        }
    }

    override fun onNavigationItemSelected(position: Int): Boolean {
        return when (position) {
            0, 1, 2, 3 -> {
                binding.vpHomePager.currentItem = position
                true
            }

            else -> false
        }
    }

    fun back(view: View) {
        onBack()
    }

    override fun onBackPressed() {
        onBack()
    }

    private fun onBack() {
        val alertDialog: AlertDialog = AlertDialog.Builder(this)
            .setTitle("提示")
            .setMessage("确定要退出吗")
            .setNegativeButton("取消", null)
            .setPositiveButton("退出", object : DialogInterface.OnClickListener {
                override fun onClick(p0: DialogInterface?, p1: Int) {
                    WebSocketManager.close()
                    finish()
                }
            })
            .create()
        alertDialog.show()
    }


    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<String>,
        grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)

        // Forward results to EasyPermissions
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this)
    }

    @AfterPermissionGranted(REQUEST_CODE)
    private fun methodRequiresTwoPermission() {
        val perms = arrayOf(
            Manifest.permission.CAMERA,
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,

            )
        if (EasyPermissions.hasPermissions(this, *perms)) {
            // Already have permission, do the thing
            // ...
        } else {
            // Do not have permissions, request them now
            EasyPermissions.requestPermissions(this, "音视频通话需要获取摄像头和录音权限", REQUEST_CODE, *perms)
        }
    }

}